//
//  SelectionCollectionViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/2.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "SelectionCollectionViewCell.h"
#import "SelectionTableViewCell.h"
#import "HXMacro.h"
#import "SelectionRes.h"
#import "ZWYNetTool.h"
#import "SelectionModel.h"
#import "MJRefresh.h"
#import "RestaurantDetailViewController.h"
#import "SelectionViewController.h"


#define kSelectionTableViewCellName @"SelectionTableViewCellNameReuse"
@interface SelectionCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) SelectionModel *model;

@property (nonatomic, strong) NSMutableArray<SelectionRes *> *resMutableArray;

@property (nonatomic, assign) int numOfPage;

@end

@implementation SelectionCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"SelectionTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kSelectionTableViewCellName];
    [self.contentView addSubview:_tableView];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)loadNewData {
    [self webRequest];
    [_tableView.mj_header endRefreshing];
}
- (void)loadMoreData {
    NSString *string = [NSString stringWithFormat:@"lat=&lon=&order=%ld&page=%d&provinceid=0&title=", _num, _numOfPage];
    if (_num == 2) {
        string = [NSString stringWithFormat:@"lat=38.882636&lon=121.539318&order=2&page=%d&provinceid=0&title=", _numOfPage];
//        string = @"lat=38.882636&lon=121.539318&order=2&page=1&provinceid=0&title=";
    }
    NSLog(@"%ld", _num);
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"44"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=restaurant&a=getlist" andBody:string andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [SelectionModel modelWithDic:result];
        for (SelectionRes *res in _model.res) {
            [_resMutableArray addObject:res];
        }
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
    _numOfPage ++;
}
- (void)setNum:(NSInteger)num {
    _num = num;
    if (_num == 0) {
        _num = 4;
    } else if (_num == 1) {
        _num = 5;
    } else if (_num == 3) {
        _num = 3;
    }
    [self webRequest];
}
- (void)webRequest {
    NSString *string = [NSString stringWithFormat:@"lat=&lon=&order=%ld&page=1&provinceid=0&title=", _num];
    if (_num == 2) {
        string = @"lat=38.882636&lon=121.539318&order=2&page=1&provinceid=0&title=";
    }
    NSLog(@"%ld", _num);
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"44"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=restaurant&a=getlist" andBody:string andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [SelectionModel modelWithDic:result];
        _resMutableArray = _model.res.mutableCopy;
        _numOfPage = 2;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
}

//- (void)setArray:(NSArray<SelectionRes *> *)array {
//    _array = array;
//    NSLog(@"%ld", array.count);
//    [_tableView reloadData];
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _resMutableArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH / 600 * 338 + 56;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSelectionTableViewCellName];
    cell.res = _resMutableArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RestaurantDetailViewController *vc = [[RestaurantDetailViewController alloc] init];
    SelectionViewController *homeVC = (SelectionViewController *)self.nextResponder.nextResponder.nextResponder;
    vc.numString = _resMutableArray[indexPath.row].sel_id;
    [homeVC.navigationController pushViewController:vc animated:YES];
}
@end
